class Relation

  def self.many_to_many(options)
    $child_connection = options[:parent_connection]
    $parent_connection = options[:child_connection]
    options.each{|key,value|
      if key.to_s != "parent_connection" && key.to_s != "child_connection" 
        first_table = key[0]
        first_table_primary_key = key[1]
        if !key[2].nil?
          first_table_connection = key[2]
        else
          first_table_connection = $parent_connection
        end
        second_table = value[0]
        second_table_primary_key = value[1]
        if !value[2].nil?
          second_table_connection = value[2]
        else
          second_table_connection = $child_connection
        end    
          m_to_m_transform(first_table,first_table_primary_key,first_table_connection,second_table,second_table_primary_key,second_table_connection)
        end  
      }
    end      
  
  
    def self.one_to_many(options={})
      # Extracted all the required options.
      child_connection = options[:child_connection]
      parent_connection = options[:parent_connection]
      parent_table_array = options[:parent_table].split
      parent_table = parent_table_array[0..(parent_table_array.index("with")-1)].join("_")
      parent_table_primary_key = parent_table_array[(parent_table_array.index("with")+1)..parent_table_array.length-1].join("_")
      
      # Handling the primary key.
      col_specs = handle_primary_key(parent_table_primary_key,parent_table,parent_connection)
      primary_key = get_primary_key(parent_table,parent_connection)
      
      # Creating the child tables and keys.  
      options.each{|key,value|
        child_table_connection = ""
        if !(["parent_connection","child_connection","parent_table"].include? key.to_s)
          for child_tab_info in value
            child_connection.execute("ALTER TABLE #{child_tab_info} ADD (#{parent_table}_#{primary_key} #{col_specs})")
            child_connection.execute("ALTER TABLE #{child_tab_info} ADD FOREIGN KEY (#{parent_table}_#{primary_key}) REFERENCES #{parent_table}(#{primary_key})")
          end    
        end  
      }
    end  
 
end